package com.withoutwords.ui.custom;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class WordsLayout extends RelativeLayout implements OnClickListener {

	int highlightedColor;
	int selectedColor;
	int normalColor;
	String[] words;
	int[] txtViewWordIds;
	Context context;
	IWordsLayoutListener listener;
	int targetWidth;
	public WordsLayout(Context context)
	{
		super(context);
		init(context);
		
	}
	public WordsLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
		// TODO Auto-generated constructor stub
	}

	private void init(Context cn)
	{
		this.context = cn;
		highlightedColor = Color.GREEN;
		selectedColor = Color.RED;
		normalColor = Color.YELLOW;
	}
	public void createViews(String[] wrds,int width)
	{
		words = wrds;
		this.targetWidth = width;
		createViews();
	}
	@Override
	public void setEnabled(boolean enabled) {
		// TODO Auto-generated method stub
		super.setEnabled(enabled);
		
		
	}
	public void highlightWord(int index)
	{
		
	}
	public void showWord(int index)
	{
		
	}
	public void setListener(IWordsLayoutListener l)
	{
		listener = l;
	}
	
	static int id = 0x7f06001f;

	// Returns a valid id that isn't in use
	public int findId(){  
	    View v = findViewById(id);  
	    while (v != null){  
	        v = findViewById(++id);  
	    }  
	    return id++;  
	}
	void createViews()
	{
		
		txtViewWordIds = new int[words.length];
		
		TextView tv = new TextView(context);	
		tv.setTextAppearance(context, android.R.style.TextAppearance_Medium);
		float [] wordsWidth = new float[words.length];
		
		
	//	RelativeLayout rly = (RelativeLayout)findViewById(R.id.text_container_id);
	//	rly.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				//
		
		int tvId = 0;
		
		int width = targetWidth;
		float nOffset = 0;
		
		for(int i =0 ; i< words.length ; i++)
		{
			wordsWidth[i] = tv.getPaint().measureText(words[i]);
			float nEnd  = nOffset + wordsWidth[i]+20;// 20 for padding;
			boolean newLine = false;
			if(nEnd > width)
			{
				nOffset = 0;
				newLine = true;
			}
			else
			{
				nOffset += wordsWidth[i]+20;
			}
			TextView tvWord = new TextView(context);	
			tvWord.setTextAppearance(context, android.R.style.TextAppearance_Medium);
			tvWord.setId(findId());
			tvWord.setPadding(10,10, 10, 10);
			tvWord.setTextColor(normalColor);
			RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			//rlp.addRule(verb, anchor)
			if( i!=0 )
			{
				if(newLine)
				{
					rlp.addRule(RelativeLayout.BELOW, tvId);
					rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
				}
				else
				{
					rlp.addRule(RelativeLayout.RIGHT_OF, tvId);
					rlp.addRule(RelativeLayout.ALIGN_TOP,tvId);
				}
			}
			
			tvWord.setLayoutParams(rlp);
			tvWord.setText(words[i]);
			tvWord.setOnClickListener(this);
			this.addView(tvWord);
			
			
			//save the id for the next run
			tvId = tvWord.getId();
			txtViewWordIds[i] = tvId;

		}
	
	}
	public void setSelectedWord(int index)
	{
		TextView tv = (TextView)findViewById(txtViewWordIds[index]);	
		tv.setTextColor(selectedColor);
	}
	@Override
	public void onClick(View v) {
	
		for(int i =0 ; i< txtViewWordIds.length; i++)
		{
			TextView tv = (TextView)findViewById(txtViewWordIds[i]);
			if(v.getId() == txtViewWordIds[i])
			{	
				tv.setTextColor(selectedColor);
				listener.onWordSelected(i);
			}
			else
			{
				tv.setTextColor(normalColor);
			}
		}
		
		
	}

	}
	
	


